'''
Created on 2009-10-24

@author: Adam Kalinski
'''
import re

def parse_learning_set(file, ftype=float):
    '''Returns 2-tuple of matrix and a solution to it'''
    matrix = []
    solution = []
    
    with open(file, "r") as f:
        for line in f:
            if line.startswith('#'): continue
            p = re.compile("\[(.*?)\]\s*?")
            l = p.findall(line)
            matrix.append([ftype(v) for v in l[0].split(';')])
            solution.append([ftype(v) for v in l[1].split(';')])
    
    return {'learning_set' : matrix,
            'solutions' : solution}

def parse_testing_set(file, ftype=float):
    matrix = []
    
    with open(file, "r") as f:
        for line in f:
            if line.startswith('#'): continue
            p = re.compile("\[(.*?)\]\s*?")
            l = p.findall(line)
            matrix.append([ftype(v) for v in l[0].split(';')])
    
    return matrix

if __name__ == "__main__":
    print parse_learning_set('learning/NXOR.txt')